program CaptureReadyEconomics;

{*******************************************************}
{       Option Value of CCS application in power plants }
{       Anh Nguyen - CIRED/CNRS                         }
{       02/2013                                         }
{*******************************************************}

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
AverageCapacityLoad             : array[0..50] of real;        //%,
AfterCashFlow                   : array[0..50] of real;        //$,
cf_retro                        : array[0..50] of real;        //$, after-tax randomized cash flow at year t with retrofitting to Capture
cf_no_retro                     : array[0..50] of real;        //$, after-tax randomized cash flow at year t without retrofitting to Capture
CashOperatingExpenses           : array[0..50] of real;        //$,
YearIncludeCCS                  : integer;                     //
CoalPrice                       : array[0..50] of real;        //$/ton,
CarbonPrice                     : array[0..50] of real;        //$/ton,
CarbonCost                      : array[0..50] of real;        //$/ton,
CCSCost                         : array[0..50] of real;        //$,
CCSCostFactor                   : real;                        //$/tCO2,
DepreciationCharge              : array[0..50] of real;        //$,
DepreciationSchedule            : integer;                     //year,
Emission                        : array[0..50] of real;        //tonCO2,
ElectricityPrice                : array[0..50] of real;        //$/MWh,
ElectricityGeneration           : array[0..50] of real;        //MWh,
PowerEfficiency                 : array[0..2,0..50] of real;   //%,
EmissoinFactor                  : real;                        //tCO2/MWh,
EndYear                         : integer;                     //   end-year
FixedCapital                    : array[0..1] of real;         //$,
FixedCapitalFactor              : real;                        //$/MWh,
CCSFixedCapital                 : array[0..1] of real;         //$,
FuelCost                        : array[0..50] of real;        //$,
HeatingValueofCoal              : real;                        //Gj/ton,
i, j, CaptureReady              : integer;
InflationRate                   : real;                        //%,
LifeTime                        : integer;                     //year,
NPV                             : real;                        //$,
NPVwithOption                   : real;                        //$,
NPVwithoutOption                : real;                        //$,
NetWorkingCapital               : real;                        //$,
OMCostFactor                    : real;                        //$/MWh,
AnnualOMCost                    : array[0..50] of real;        //$,
Outlay                          : real;                        //$,
PowerCapacity                   : real;                        //MW,
RateOfReturn                    : real;                        //%,
RevenueFromElectricitySales     : array[0..50] of real;        //$,
RetrofitYear                    : integer;                     //
Tax                             : real;                        //%,
StartOperationYear              : integer;                     //
AfterTaxNonOperatingCashFlow    : real;                        //$,
ValueOption                     : real;                        //$,

const HoursAYear: integer = 8760;
      MWhToGJ: real = 3.6;

//define procedures

procedure  NetPresentValue(j, u: integer);
  var v: integer;
  begin
      NPV := 0;
      Outlay:= FixedCapital[j]*1.05 + u*CCSFixedCapital[j];
      For v:=1 to LifeTime do
          begin
              AfterCashFlow[v]:=(RevenueFromElectricitySales[v]-CashOperatingExpenses[v]-DepreciationCharge[v])*(1-tax) + DepreciationCharge[v];
              NPV := NPV + AfterCashFlow[v]/exp(v*ln(RateOfReturn+1));
          end;
      NPV := NPV - Outlay + AfterTaxNonOperatingCashFlow/exp(LifeTime*ln(RateOfReturn+1));
  end;


procedure ElectricitySales;
  begin
    ElectricityGeneration[i]:=PowerCapacity*HoursAYear*AverageCapacityLoad[i];
    RevenueFromElectricitySales[i]:=ElectricityGeneration[i]*ElectricityPrice[i];
  end;


procedure OperationExpensesNoCCS(j: integer);
  begin
    FuelCost[i]:=((ElectricityGeneration[i]*MWhToGJ)/(PowerEfficiency[j,i]*HeatingValueofCoal))*CoalPrice[i];
    CarbonCost[i]:=ElectricityGeneration[i]*EmissoinFactor*CarbonPrice[i];
    AnnualOMCost[i]:=ElectricityGeneration[i]*OMCostFactor;
    CashOperatingExpenses[i]:= FuelCost[i]+CarbonCost[i]+AnnualOMCost[i];
  end;

procedure OperationExpensesWithCCS(j: integer);
  begin
    FuelCost[i]:=((ElectricityGeneration[i]*MWhToGJ)/(PowerEfficiency[j,i]*HeatingValueofCoal))*CoalPrice[i];
    CarbonCost[i]:=ElectricityGeneration[i]*EmissoinFactor*CCSCostFactor;
    AnnualOMCost[i]:=ElectricityGeneration[i]*OMCostFactor;
    CashOperatingExpenses[i]:= FuelCost[i]+CarbonCost[i]+AnnualOMCost[i];
  end;

procedure  ShowNPV;
  begin
      writeln('NPV($)= '+ FloatToStr(NPV));
  end;

procedure Depreciation;
  var k: integer;
  begin
      For k:=1 to DepreciationSchedule do
          DepreciationCharge[k]:=FixedCapital[CaptureReady]/DepreciationSchedule;
      If (YearIncludeCCS>StartOperationYear)and(YearIncludeCCS<EndYear) then
          For k:=(YearIncludeCCS-StartOperationYear +1) to (YearIncludeCCS-StartOperationYear +1 + DepreciationSchedule) do
              DepreciationCharge[k]:=DepreciationCharge[k]+CCSFixedCapital[CaptureReady]/DepreciationSchedule;
  end;

begin

//data entry

 {write('Enter power capacity (MW): ');
 readln(power);
 write('Enter start-operation year of the power plant: ');
 readln(ty);
 write('Enter end-year of the power plant: ');
 readln(EndYear);
 n:=EndYear-ty +1;  //calculate life-time of the plant
 write('Enter depreciation schedule(number of year): ');
 readln(DepreciationSchedule);}

 PowerCapacity:=600;
 StartOperationYear:=2016;
 EndYear:=2050;
 LifeTime:=EndYear-StartOperationYear+1;
 DepreciationSchedule:=10;

 FixedCapitalFactor := 800000;
 FixedCapital[0]:= PowerCapacity*FixedCapitalFactor;
 FixedCapital[1]:= FixedCapital[0]*1.1;
 CCSFixedCapital[0]:=FixedCapital[0]*0.4;
 CCSFixedCapital[1]:=FixedCapital[1]*0.2;

 j:=0;                             //enter average capacity load without CCS
 AverageCapacityLoad[1]:=0.85;
 For i:=1 to LifeTime do
    begin
        AverageCapacityLoad[i+1]:=AverageCapacityLoad[i];
        j:=j+1;
        if j>4 then
            begin
                AverageCapacityLoad[i+1]:=AverageCapacityLoad[i+1]-0.05;
                j:=0;
            end;
    end;

 For i:=1 to LifeTime do
    begin
        PowerEfficiency[2,i]:=0.42;
        PowerEfficiency[0,i]:=PowerEfficiency[2,i]-0.095;                      //enter average capacity load, no CR with CCS
        PowerEfficiency[1,i]:=PowerEfficiency[2,i]-0.085;                      //enter average capacity load, CR with CCS
    end;

 InflationRate:=0.05;

 ElectricityPrice[1]:=100;
 for i:=1 to LifeTime-1 do
    ElectricityPrice[i+1]:=ElectricityPrice[i]*(1+InflationRate);

 HeatingValueofCoal:=22.51;
 OMCostFactor:=0.25;
 EmissoinFactor:=0.79;
 CCSCostFactor:=5;
 Tax:=0.25;
 RateOfReturn:=0.1;
 CarbonPrice[1]:=20;
 for i:=1 to LifeTime-1 do
    CarbonPrice[i+1]:=CarbonPrice[i]+0.5;
 CoalPrice[1]:=70;
 for i:=1 to LifeTime-1 do CoalPrice[i+1]:=CoalPrice[i]+0.5;

 //main calculations

 Repeat
    begin
        write('Enter 0 to not select or 1 to select CCS-Ready at the beginning: ');
        readln(CaptureReady);
        write('Enter the year to include CCS technology in the power plant: ');
        readln(YearIncludeCCS);

        If (CaptureReady = 0) then
            if (YearIncludeCCS>StartOperationYear)and(YearIncludeCCS<EndYear) then
                begin
                      Depreciation;
                      For i:=1 to (YearIncludeCCS-StartOperationYear) do                     //period before CCS included
                          begin
                              ElectricitySales;
                              OperationExpensesNoCCS(2);
                          end;
                      For i:=(YearIncludeCCS-StartOperationYear +1) to LifeTime do
                          begin
                              ElectricitySales;
                              OperationExpensesWithCCS(0);
                          end;
                      NetPresentValue(0,1);
                      ShowNPV;
                end
            else
                begin
                    Depreciation;
                    For i:=1 to LifeTime do
                        begin
                            ElectricitySales;
                            OperationExpensesNoCCS(2)
                        end;
                    NetPresentValue(0,0);
                    ShowNPV;
                end;

        If (CaptureReady = 1) then
            if (YearIncludeCCS>StartOperationYear)and(YearIncludeCCS<EndYear) then
                begin
                    Depreciation;
                    For i:=1 to (YearIncludeCCS-StartOperationYear) do                     //period before CCS included
                        begin
                            ElectricitySales;
                            OperationExpensesNoCCS(2);
                        end;
                    For i:=(YearIncludeCCS-StartOperationYear +1) to LifeTime do
                        begin
                            ElectricitySales;
                            OperationExpensesWithCCS(1);
                        end;
                    NetPresentValue (1,1);
                    ShowNPV;
                end
            else
                begin
                    Depreciation;
                    For i:=1 to LifeTime do
                        begin
                            ElectricitySales;
                            OperationExpensesNoCCS(2);
                        end;
                    NetPresentValue (1,0);
                    ShowNPV;
                end;
        i:=1;
        write('Do you want to continue (Yes=1, No=0)?:  ');
        readln(i);
        writeln(' ');
    end
 until (i=0);

 //value_option:= (npv_woption - npv_wtoption); //calculate value option
 readln;

end.
